﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar.SuperGrid;
using SyncConfig.Dialog;
using DevComponents.AdvTree;
using DevComponents.DotNetBar;
using AppCommon.Controls;
using AppCommon;

namespace SyncConfig.Controls
{
    public partial class ScopeInfoExList : UserControlBase
    {
        DataTable tblScopeType = new DataTable();

        public ScopeInfoExList()
        {
            InitializeComponent();


            tblScopeType.Columns.Add("ID", typeof(int));
            tblScopeType.Columns.Add("Name", typeof(string));
            tblScopeType.Rows.Add(1, "Danh mục");
            tblScopeType.Rows.Add(2, "Phiếu");

            GridColumn col = superGridControl1.PrimaryGrid.Columns["ScopeType"];
            GridComboBoxExEditControl ec = col.EditControl as GridComboBoxExEditControl;
            ec.DataSource = tblScopeType;
            ec.DisplayMember = "Name";
            ec.ValueMember = "ID";

            ec = col.RenderControl as GridComboBoxExEditControl;
            ec.DataSource = tblScopeType;
            ec.DisplayMember = "Name";
            ec.ValueMember = "ID";

            col = superGridControl1.PrimaryGrid.Columns["Up"];
            GridButtonXEditControl btn = col.EditControl as GridButtonXEditControl;
            btn.Image = Properties.Resources.UpArrowShort;
            btn.Click += btnUp_Click;
            btn = col.RenderControl as GridButtonXEditControl;
            btn.Image = Properties.Resources.UpArrowShort;
            btn.Click += btnUp_Click;

            col = superGridControl1.PrimaryGrid.Columns["Down"];
            btn = col.EditControl as GridButtonXEditControl;
            btn.Image = Properties.Resources.DownArrowShort;
            btn.Click += btnDown_Click;
            btn = col.RenderControl as GridButtonXEditControl;
            btn.Image = Properties.Resources.DownArrowShort;
            btn.Click += btnDown_Click;
        }

        public override void Close()
        {
            FrmMain.MainForm.TreeManipulation.dsSyncConfig.ScopeInfoEx.RejectChanges();
            CurrentNode = null;
        }
        public void DataBind(bool isCommond)
        {

            Tag = isCommond;
            DataView view = new DataView(FrmMain.MainForm.TreeManipulation.dsSyncConfig.ScopeInfoEx);
            view.Table.RejectChanges();
           // view.RowFilter = "IsCommon=" + isCommond.ToString();

            SyncConfigDS.ClientRow clientRow = CurrentNode.Tag as SyncConfigDS.ClientRow;
            if(clientRow!=null)
            {
                view.RowFilter += " AND ClientId='" + clientRow.ClientId.ToString() + "'";
            }
            superGridControl1.PrimaryGrid.DataSource = view;
        }

        private void btnUp_Click(object sender, EventArgs e)
        {
            if (superGridControl1.ActiveRow != null)
            {
                if (superGridControl1.ActiveRow.RowIndex == 0)
                    return;
                int currentIndex = superGridControl1.ActiveRow.RowIndex;
                GridRow currentRow = superGridControl1.PrimaryGrid.Rows[superGridControl1.ActiveRow.RowIndex] as GridRow;

                GridRow preRow = superGridControl1.PrimaryGrid.Rows[superGridControl1.ActiveRow.RowIndex - 1] as GridRow;

                object ordinal1 = preRow.Cells["Ordinal"].Value;
                preRow.Cells["Ordinal"].Value = currentRow.Cells["Ordinal"].Value;
                currentRow.Cells["Ordinal"].Value = ordinal1;
                currentRow.SetActive();

                //currentRow = superGridControl2.PrimaryGrid.Rows[currentIndex - 1] as GridRow;
                superGridControl1.PrimaryGrid.SetSort(superGridControl1.PrimaryGrid.Columns["Ordinal"], SortDirection.Ascending);
                superGridControl1.PrimaryGrid.InvalidateLayout();
                superGridControl1.PrimaryGrid.SetActiveRow(currentRow);

            }
        }

        private void btnDown_Click(object sender, EventArgs e)
        {
            if (superGridControl1.ActiveRow != null)
            {
                if (superGridControl1.ActiveRow.RowIndex == superGridControl1.PrimaryGrid.Rows.Count - 1)
                    return;
                int currentIndex = superGridControl1.ActiveRow.RowIndex;
                GridRow currentRow = superGridControl1.PrimaryGrid.Rows[superGridControl1.ActiveRow.RowIndex] as GridRow;

                GridRow preRow = superGridControl1.PrimaryGrid.Rows[superGridControl1.ActiveRow.RowIndex + 1] as GridRow;

                object ordinal1 = preRow.Cells["Ordinal"].Value;
                preRow.Cells["Ordinal"].Value = currentRow.Cells["Ordinal"].Value;
                currentRow.Cells["Ordinal"].Value = ordinal1;
                superGridControl1.PrimaryGrid.InvalidateLayout();
                superGridControl1.PrimaryGrid.SetSort(superGridControl1.PrimaryGrid.Columns["Ordinal"], SortDirection.Ascending);
                superGridControl1.PrimaryGrid.SetActiveRow(currentRow);
                //syncScopeBindingSource.ResetBindings(true);
                //currentRow = superGridControl2.PrimaryGrid.Rows[currentIndex + 1] as GridRow;
                // 


            }
        }
        void CreateContextMenu()
        {
            //// Create context menu item that is assigned to controls or items
            //ButtonItem contextMenu = new ButtonItem("myContextMenuItemName");
            //// Create menu items
            //ButtonItem menuItem = new ButtonItem("menuItemName1");
            //menuItem.Text = "Context Menu Item &amp;1";
            //menuItem.Click += new EventHandler(MenuItemClick);
            //// Add item to Context Menu
            //contextMenu.SubItems.Add(menuItem);

            //// Create second context menu item
            //menuItem = new ButtonItem("menuItemName1");
            //menuItem.Text = "Context Menu Item &amp;2";
            //menuItem.Click += new EventHandler(MenuItemClick);
            //// Add item to Context Menu
            //contextMenu.SubItems.Add(menuItem);

            //// Create third context menu item
            //menuItem = new ButtonItem("menuItemName1");
            //menuItem.Text = "Context Menu Item &amp;3";
            //menuItem.Click += new EventHandler(MenuItemClick);
            //// Add item to Context Menu
            //contextMenu.SubItems.Add(menuItem);

            //// Add Context Menu to contextMenuBar1
            //contextMenuBar1.Items.Add(contextMenu);

            //// Assign context menu to text-box
            //contextMenuBar1.SetContextMenuEx(textBoxX1, contextMenu);
        }
        private void btnThemMoi_Click(object sender, EventArgs e)
        {
            AddScopeInfoEx ctrl = new AddScopeInfoEx();
            BaseDialog frm = new BaseDialog();
            try
            {
                frm.Size = new Size(ctrl.Size.Width + 16, ctrl.Size.Height + 40);
                ctrl.Dock = DockStyle.Fill;
                ctrl.BackColor = Color.Transparent;                
                ctrl.BindingNew(CurrentNode);
                ctrl.IsCommon = (bool)Tag;
                frm.panelEx1.Controls.Add(ctrl);
                frm.ShowDialog();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                frm.Dispose();
            }

        }

        private void superGridControl1_ClientSizeChanged(object sender, EventArgs e)
        {
            //  GridColumn col = superGridControl1.PrimaryGrid.Columns["Up"];
            // // col.Bounds.Location;
            //  Point p = col.Bounds.Location;
            //    //superGridControl1.PointToScreen(col.Bounds.Location);
            ////p = this.PointToClient(p);
            //btnUp.Location = new Point(p.X+superGridControl1.Location.X, btnUp.Location.Y);

        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                DataView view = superGridControl1.PrimaryGrid.DataSource as DataView;
                FrmMain.MainForm.TreeManipulation.AdapterScopeInfoEx.Update((SyncConfigDS.ScopeInfoExDataTable)view.Table);
                view.Table.AcceptChanges();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void biViewDetail_Click(object sender, EventArgs e)
        {
            if (superGridControl1.PrimaryGrid.ActiveRow != null)
            {
                SyncConfigDS.ScopeInfoExRow row = superGridControl1.GetCurrentRow<SyncConfigDS.ScopeInfoExRow>();
                Node[] existsNode = CurrentNode.Nodes.Find(row.ScopeName, true);
                if (existsNode.Length > 0)
                {
                    BaseDialog frm = new BaseDialog();
                    try
                    {
                        AddScopeInfoEx ctrl = new AddScopeInfoEx();
                        frm.Size = new Size(ctrl.Width + 20, ctrl.Height + 60);
                        ctrl.BindingInfo(existsNode[0]);
                        frm.panelEx1.Controls.Add(ctrl);
                        frm.ShowDialog();
                        ctrl.Close();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                    finally
                    {

                        frm.Dispose();
                    }
                }
            }
        }

        private void bbiTableList_Click(object sender, EventArgs e)
        {
            if (superGridControl1.PrimaryGrid.ActiveRow != null)
            {
                SyncConfigDS.ScopeInfoExRow row = superGridControl1.GetCurrentRow<SyncConfigDS.ScopeInfoExRow>();

                Node[] existsNode = CurrentNode.Nodes.Find(row.ScopeName, true);
                if (existsNode.Length > 0)
                {

                    BaseDialog frm = new BaseDialog();
                    try
                    {
                        TableOfScopeList ctrl = new TableOfScopeList();
                        frm.Size = new Size(ctrl.Width + 20, ctrl.Height + 60);
                        ctrl.CurrentNode = (existsNode[0].Nodes.Find(AdvTreeManipulation.TableOfScopeName, false)[0]);
                        ctrl.DataBind();
                        frm.panelEx1.Controls.Add(ctrl);
                        frm.ShowDialog();
                        ctrl.Close();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                    finally
                    {

                        frm.Dispose();
                    }
                }
            }
        }

        private void superGridControl1_RowDoubleClick(object sender, GridRowDoubleClickEventArgs e)
        {

            biViewDetail_Click(sender, e);
        }

    }
}
